package com.xx.controller;

import com.xx.entity.Book;
import com.xx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 书籍控制器
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    BookService bookService;

    //获取全部书籍  作者 / 书籍名称
    @GetMapping
    public List<Book> getList(String bookName,String author){
        return bookService.getList(bookName,author);
    }

    //获取书籍的详情
    @GetMapping("{id}")
    public Book getBookInfo(@PathVariable("id") Integer bookId){
        return bookService.getBookInfo(bookId);
    }

    //新增书籍
    @PostMapping
    public String saveBook(@RequestBody Book book){
        int count = bookService.saveBook(book);
        return count>0?"新增成功":"新增失败";
    }

    //修改书籍
    @PutMapping
    public String updateBook(Book book){
        int count = bookService.updateBook(book);
        return count>0?"修改成功":"修改失败";
    }

    //删除书籍
    @DeleteMapping("{bookId}")
    public String delBook(@PathVariable Integer bookId){
        int count = bookService.delBook(bookId);
        return count>0?"删除成功":"删除失败";
    }
}
